
package com.kujie.ai.dify.core;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kujie.ai.dify.api.*;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;

import static com.kujie.ai.dify.core.HttpInterfaceUtil.resolveToken;

/**
 * dify-api配置
 */
@RequiredArgsConstructor
@EnableConfigurationProperties(DifyProperties.class)
@Conditional(DifyValidationAutoConfiguration.EnableDifyApiCondition.class) // 条件控制
public class DifyApiAutoConfiguration {
    private final DifyProperties difyProperties;
    private final ObjectMapper objectMapper;

    /**
     * dify聊天api
     *
     * @return 想定远程服务
     */
    @Bean
    @ConditionalOnMissingBean(DifyChatApi.class)
    @ConditionalOnProperty(prefix = "dify", name = "chat-key")
    public DifyChatApi difyChatApi() {
        return HttpInterfaceUtil.createRestService(
                difyProperties.getBaseUrl(),
                difyProperties.getChatKey(),
                objectMapper,
                null,
                DifyChatApi.class);
    }

    /**
     * 声明流调用的chat-api
     *
     * @return client
     */
    @Bean
    @ConditionalOnProperty(prefix = "dify", name = "chat-key")
    @ConditionalOnMissingBean(DifyChatSseApi.class)
    public DifyChatSseApi difyChatSseApi() {
        return HttpInterfaceUtil.createWebService(
                difyProperties.getBaseUrl(),
                difyProperties.getChatKey(),
                objectMapper,
                null,
                DifyChatSseApi.class);
    }

    /**
     * dify知识库api
     *
     * @return 想定远程服务
     */
    @Bean
    @ConditionalOnMissingBean(DifyDatasetApi.class)
    @ConditionalOnProperty(prefix = "dify", name = "dataset-key")
    public DifyDatasetApi difyDatasetApi() {
        return HttpInterfaceUtil.createRestService(
                difyProperties.getBaseUrl(),
                difyProperties.getDatasetKey(),
                objectMapper,
                null,
                DifyDatasetApi.class);
    }

    /**
     * dify文本api
     *
     * @return 想定远程服务
     */
    @Bean
    @ConditionalOnMissingBean(DifyTextApi.class)
    @ConditionalOnProperty(prefix = "dify", name = "text-key")
    public DifyTextApi difyTextApi() {
        return HttpInterfaceUtil.createRestService(
                difyProperties.getBaseUrl(),
                difyProperties.getTextKey(),
                objectMapper,
                null,
                DifyTextApi.class);
    }

    /**
     * 文本sse-api
     *
     * @return api
     */
    @Bean
    @ConditionalOnMissingBean(DifyTextSseApi.class)
    @ConditionalOnProperty(prefix = "dify", name = "text-key")
    public DifyTextSseApi difyTextSseApi() {
        return HttpInterfaceUtil.createWebService(
                difyProperties.getBaseUrl(),
                difyProperties.getTextKey(),
                objectMapper,
                null,
                DifyTextSseApi.class);
    }

    /**
     * dify工作流api
     *
     * @return 远程服务
     */
    @Bean
    @ConditionalOnMissingBean(DifyWorkflowApi.class)
    @ConditionalOnProperty(prefix = "dify", name = "workflow-key")
    public DifyWorkflowApi difyWorkflowApi() {
        return HttpInterfaceUtil.createRestService(
                difyProperties.getBaseUrl(),
                difyProperties.getWorkflowKey(),
                objectMapper,
                null,
                DifyWorkflowApi.class);
    }

    /**
     * 工作流sse-api
     *
     * @return api
     */
    @Bean
    @ConditionalOnMissingBean(DifyWorkflowSseApi.class)
    @ConditionalOnProperty(prefix = "dify", name = "workflow-key")
    public DifyWorkflowSseApi difyWorkflowSseApi() {
        return HttpInterfaceUtil.createWebService(
                difyProperties.getBaseUrl(),
                difyProperties.getWorkflowKey(),
                objectMapper,
                null,
                DifyWorkflowSseApi.class);
    }
}